package com.ruoyi.project.channel.bankInfo.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.channel.channelOrder.domain.ChannelOrder;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.channel.bankInfo.domain.BankInfo;
import com.ruoyi.project.channel.bankInfo.service.IBankInfoService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 银行卡信息操作处理
 *
 * @author zero
 * @date 2019-05-04
 */
@Controller
@RequestMapping("/channel/bankInfo")
public class BankInfoController extends BaseController {
    private String prefix = "channel/bankInfo";

    @Autowired
    private IBankInfoService bankInfoService;

    @RequiresPermissions("channel:bankInfo:view")
    @GetMapping()
    public String bankInfo() {
        return prefix + "/bankInfo";
    }

    /**
     * 查询银行卡列表
     */
    @RequiresPermissions("channel:bankInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BankInfo bankInfo) {
        startPage();

        // 取身份信息
        User user = getSysUser();
        String roleKey = ShiroUtils.getSysUser().getRoles().get(0).getRoleKey();
        List<BankInfo> list = new ArrayList<>();
        if (roleKey.equals("mall")) {
            bankInfo.setUserId(user.getUserId());
            list = bankInfoService.selectBankInfoList(bankInfo);
        } else if (roleKey.equals("admin") || roleKey.equals("admin2")) {
            list = bankInfoService.selectBankInfoList(bankInfo);
        }
        return getDataTable(list);
    }

    /**
     * 获取当前银行卡信息
     *
     * @return
     */
    @GetMapping("/userBankInfo")
    @ResponseBody
    public AjaxResult getBankInfo() {
        // 取身份信息
        User user = getSysUser();
        BankInfo search = new BankInfo();
        search.setUserId(user.getUserId());
        List<BankInfo> list = bankInfoService.selectBankInfoList(search);
        if (list.size() > 0) {
            return AjaxResult.success("success", list.get(0));
        } else {
            bankInfoService.insertBankInfo(search);
            return AjaxResult.success("success", search);
        }
    }

    /**
     * 获取当前银行卡信息
     *
     * @return
     */
    @GetMapping("/userBankListInfo")
    @ResponseBody
    public AjaxResult getBankListInfo() {
        // 取身份信息
        User user = getSysUser();
        BankInfo search = new BankInfo();
        search.setUserId(user.getUserId());
        List<BankInfo> list = bankInfoService.selectBankInfoList(search);
        return AjaxResult.success("success", list);
    }

    /**
     * 导出银行卡列表
     */
    @RequiresPermissions("channel:bankInfo:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BankInfo bankInfo) {
        // 取身份信息
        User user = getSysUser();
        String roleKey = ShiroUtils.getSysUser().getRoles().get(0).getRoleKey();
        List<BankInfo> list = new ArrayList<>();
        if (roleKey.equals("mall")) {
            bankInfo.setUserId(user.getUserId());
            list = bankInfoService.selectBankInfoList(bankInfo);
        } else if (roleKey.equals("admin") || roleKey.equals("admin2")) {
            list = bankInfoService.selectBankInfoList(bankInfo);
        }
        ExcelUtil<BankInfo> util = new ExcelUtil<BankInfo>(BankInfo.class);
        return util.exportExcel(list, "bankInfo");
    }

    /**
     * 新增银行卡
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存银行卡
     */
    @RequiresPermissions("channel:bankInfo:add")
    @Log(title = "银行卡", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BankInfo bankInfo) {
        return toAjax(bankInfoService.insertBankInfo(bankInfo));
    }

    /**
     * 修改银行卡
     */
    @GetMapping("/edit/{bankId}")
    public String edit(@PathVariable("bankId") String bankId, ModelMap mmap) {
        BankInfo bankInfo = bankInfoService.selectBankInfoById(bankId);
        mmap.put("bankInfo", bankInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存银行卡
     */
    @RequiresPermissions("channel:bankInfo:edit")
    @Log(title = "银行卡", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BankInfo bankInfo) {
        return toAjax(bankInfoService.updateBankInfo(bankInfo));
    }

    /**
     * 修改保存银行卡
     */
    @Log(title = "银行卡", businessType = BusinessType.UPDATE)
    @PostMapping("/editSelf")
    @ResponseBody
    public AjaxResult editSelfSave(@RequestBody BankInfo bankInfo) {
        // 只能修改当前操作人员的id
        BankInfo search = new BankInfo();
        search.setUserId(getSysUser().getUserId());
        List<BankInfo> list = bankInfoService.selectBankInfoList(search);
        bankInfo.setUserId(getSysUser().getUserId());
        bankInfo.setBankId(list.get(0).getBankId());
        return toAjax(bankInfoService.updateBankInfo(bankInfo));
    }

    /**
     * 修改保存银行卡
     */
    @Log(title = "银行卡", businessType = BusinessType.INSERT)
    @PostMapping("/addSelf")
    @ResponseBody
    public AjaxResult addSelfSave(@RequestBody BankInfo bankInfo) {
        // 只能修改当前操作人员的id
        bankInfo.setUserId(getSysUser().getUserId());
        BankInfo search = new BankInfo();
        search.setBankNo(bankInfo.getBankNo());
        List<BankInfo> bankInfos = bankInfoService.selectBankInfoList(search);
        if (bankInfos.size() > 0) {
            return AjaxResult.error("wallet NO. has already exists");
        }
        return toAjax(bankInfoService.insertBankInfo(bankInfo));
    }

    /**
     * 修改保存银行卡
     */
    @Log(title = "银行卡", businessType = BusinessType.INSERT)
    @PostMapping("/updateSelf")
    @ResponseBody
    public AjaxResult updateSelfSave(BankInfo bankInfo) {
        // 只能修改当前操作人员的id
        BankInfo search = new BankInfo();
        search.setUserId(bankInfo.getUserId());
        List<BankInfo> bankInfos = bankInfoService.selectBankInfoList(search);
        if (!bankInfos.isEmpty()) {
            bankInfos.get(0).setBankNo(bankInfo.getBankNo());
            return toAjax(bankInfoService.updateBankInfo(bankInfos.get(0)));
        } else {
            return toAjax(bankInfoService.insertBankInfo(bankInfo));
        }
    }

    /**
     * 删除银行卡
     */
    @RequiresPermissions("channel:bankInfo:remove")
    @Log(title = "银行卡", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(bankInfoService.deleteBankInfoByIds(ids));
    }

}
